﻿using System;
using System.Collections.Generic;
using System.Text;
using EA;


using System.IO;

using System.Threading;
using System.Runtime.InteropServices;



namespace HSDc.EA.WorkflowDefinitionsTranformPlugIn
{
     [ComVisible(true)]
    public class WorkflowDefTransformMgr
    {
        private bool m_ShowFullMenus = false;
        private string id = string.Empty;

        //Called Before EA starts to check Add-In Exists
        public String EA_Connect(Repository Repository)
        {
            //No special processing required.
            id = Guid.NewGuid().ToString();
            return "a string";
        }

        //Called when user Click Add-Ins Menu item from within EA.
        //Populates the Menu with our desired selections.
        public object EA_GetMenuItems(Repository Repository, string Location, string MenuName)
        {
            Package aPackage = Repository.GetTreeSelectedPackage();
            switch (MenuName)
            {
                case "":
                    return "-&Workflow Upload";
                case "-&Workflow Upload":
                    string[] ar = { "&Upload to RingleFlow" };
                    return ar;
            }
            return "";
        }

        //Sets the state of the menu depending if there is an active project or not
        bool IsProjectOpen(Repository Repository)
        {
            try
            {
                Collection c = Repository.Models;
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Called once Menu has been opened to see what menu items are active.
        public void EA_GetMenuState(Repository Repository, string Location, string MenuName, string ItemName, ref bool IsEnabled, ref bool IsChecked)
        {
            if (IsProjectOpen(Repository))
            {
                if (ItemName == "Upload to RingleFlow")
                    IsEnabled = m_ShowFullMenus;
            }
            else
                // If no open project, disable all menu options
                IsEnabled = false;
        }

        //Called when user makes a selection in the menu.
        //This is your main exit point to the rest of your Add-in
        public void EA_MenuClick(Repository Repository, string Location, string MenuName, string ItemName)
        {
            switch (ItemName)
            {
                case "&Menu1":
                    //temp

                       if (Resources.EAResource.CurrentRepository(id)==null)
                            Resources.EAResource.CurrentRepository(id, Repository);

                       Resources.EAResource.CurrentDiagramId(id, ((Diagram)Repository.GetTreeSelectedObject()).DiagramID);

                    HSDc.DefinitionTransformer.Controller.TransformController ctrl = new HSDc.DefinitionTransformer.Controller.TransformController();
                    ctrl.TransformDiagram(Resources.EAResource.CurrentDiagramId(id), id);
                    break;
                case "&Upload to RingleFlow":

                     if (Resources.EAResource.CurrentRepository(id)==null) Resources.EAResource.CurrentRepository(id, Repository);

                    Diagram diagram = ((Diagram)Repository.GetTreeSelectedObject());
                    Resources.EAResource.CurrentDiagramId(id, diagram.DiagramID);

                    HSDc.DefinitionTransformer.Controller.TransformController ctrl2 = new HSDc.DefinitionTransformer.Controller.TransformController();
                    DefinitionTransformer.DTO.ProcessDefinition proc = ctrl2.TransformDiagram(Resources.EAResource.CurrentDiagramId(id), id);

                    Transform tranFrm = new Transform(proc);
                    tranFrm.ProcessName = diagram.Name;
                    tranFrm.ProcessVersion = diagram.Version;
                    tranFrm.ShowDialog();
                    if (tranFrm.IsConfigFinished){
                        try
                        {
                            string processId = ctrl2.CommitTransform(tranFrm.ProcessDefinition);
                            System.Windows.Forms.MessageBox.Show("Transform Successfully!, Process ID is " + processId);
                        }
                        catch (Exception)
                        {
                            System.Windows.Forms.MessageBox.Show("Transform Failed!");
                        }
                    } 
               

                    break;
                case "About...":
                  
                    break;
            }
        }

     


    }
}
